package cn.mxz.robots.util;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;

import cn.mxz.crawler_single.cache.Curl;

/**
 * Redis服务接口，通过Http访问数据
 * 
 * @author kangbin <kangbin@mouge.wang>
 * @since 2015-05-01
 */
public class DeprecatedRedisGK
{
	private final static String ROOT_URL = "http://cache.mengxiaozhu.cn/redis/";

	public static void set(String key, String value)
	{
		Curl.post(ROOT_URL + "string", "key=" + key + "&value=" + Base64.encodeBase64String(StringUtils.getBytesUtf8(value)));
	}

	public static String get(String key)
	{
		return Curl.get(ROOT_URL + "string/" + key);
	}

	public static void del(String key)
	{
		Curl.delete(ROOT_URL + "string/" + key);
	}

	public static String sadd(String key, String value)
	{
		return Curl.post(ROOT_URL + "set", "key=" + key + "&value=" + Base64.encodeBase64String(Base64.encodeBase64String(StringUtils.getBytesUtf8(value)).getBytes()));
	}

	public static void hset(String key, String field, String value)
	{
		Curl.post(ROOT_URL + "hashmap", "key=" + key + "&field=" + field + "&value=" + Base64.encodeBase64String(Base64.encodeBase64String(StringUtils.getBytesUtf8(value)).getBytes()));
	}

	public static String hget(String key, String field)
	{
		String result = Curl.get(ROOT_URL + "hashmap/" + key + "/" + field);
		return StringUtils.newStringUtf8(Base64.decodeBase64(result));
	}

	public static String hgetAll(String key)
	{
		return Curl.get(ROOT_URL + "hashmap/" + key);
	}

}
